package com.example.designpatterns.interpreter.terminal;

import com.example.designpatterns.interpreter.Expression;
import java.util.Map;

/**
 * 变量表达式 - 终结符表达式
 * 表示一个变量，其值从上下文中获取
 */
public class VariableExpression implements Expression {
    private String variableName;

    public VariableExpression(String variableName) {
        this.variableName = variableName;
    }

    @Override
    public int interpret(Map<String, Integer> context) {
        if (context.containsKey(variableName)) {
            return context.get(variableName);
        }
        return 0; // 如果变量未定义，则返回0
    }
    
    @Override
    public String toString() {
        return variableName;
    }
} 